package com.inspur.resourcequery.dbHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.inspur.common.MyApplication;

public class CacheService {

	private Context context;
	
	private final String REPORT_TABLE_NAME = "RES_QUERY";
	
	private String datePattern = "yyyy-MM-dd HH:mm:ss"; 
	SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
	String userName;
	public CacheService(Context mContext){
		this.context = mContext;
		userName = ((MyApplication)((Activity)mContext).getApplication()).getUserInfo().getUseraccount();
	}
	
	/**
	 * 保存缓存数据
	 * @param cacheKey 缓存key
	 * @param cacheValue 缓存值
	 */
	public void saveCache(String cacheKey, String cacheValue){
		DBHelper dbHelper = new DBHelper(context);
		SQLiteDatabase db = dbHelper.open();
		ContentValues values = new ContentValues();
		values.put("cacheKey", cacheKey);
		values.put("cacheValue", cacheValue);
		values.put("userName", userName);
		values.put("updateDate", sdf.format(new Date()));
		db.insert(REPORT_TABLE_NAME, null, values);
		dbHelper.close(db);
	}
	
	/**
	 * 更新缓存数据
	 * @param cacheKey 缓存key
	 * @param cacheValue 缓存值
	 */ 
	public void updateCache(String cacheKey, String cacheValue){
		DBHelper dbHelper = new DBHelper(context);
		SQLiteDatabase db = dbHelper.open();
		ContentValues values = new ContentValues();
		values.put("cacheKey", cacheKey);
		values.put("cacheValue", cacheValue);
		values.put("updateDate", sdf.format(new Date()));
		db.update(REPORT_TABLE_NAME, values, "cacheKey = ? and userName = ?", new String[]{cacheKey, userName});
		dbHelper.close(db);
	}
	
	/**
	 * 查询缓存数据
	 * @param cacheKey 缓存key
	 * @return 缓存值
	 */
	public String[] getCacheWithFlag(String cacheKey){
		String cacheValue = "-1";
		String cacheFlag = "1";
		
		DBHelper dbHelper = new DBHelper(context);
		SQLiteDatabase db = dbHelper.open();
		Cursor cursor = db.rawQuery("SELECT cacheValue,updateDate FROM "+REPORT_TABLE_NAME+" WHERE cacheKey = ? and userName = ?", new String[]{cacheKey, userName});  	
		if(cursor.getCount() > 0 && cursor.moveToFirst()){
			cacheValue = cursor.getString(cursor.getColumnIndex("cacheValue"));
			try {
				String updateDate = cursor.getString(cursor.getColumnIndex("updateDate"));
				Log.e("更新时间", updateDate);
				Date date = sdf.parse(updateDate);
				cacheFlag = this.checkCacheTime(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		dbHelper.close(db);
		return new String[]{cacheFlag, cacheValue};
	}
	
	/**
	 * 查询缓存数据
	 * @param cacheKey 缓存key
	 * @return 缓存值
	 */
	public String getCache(String cacheKey){
		String cacheValue = "-1";
		DBHelper dbHelper = new DBHelper(context);
		SQLiteDatabase db = dbHelper.open();
		Cursor cursor = db.rawQuery("SELECT cacheValue,updateDate FROM "+REPORT_TABLE_NAME+" WHERE cacheKey = ? and userName = ?", new String[]{cacheKey, userName});  	
		if(cursor.getCount() > 0 && cursor.moveToFirst()){
			cacheValue = cursor.getString(cursor.getColumnIndex("cacheValue"));
		}
		dbHelper.close(db);
		return cacheValue;
	}

    /**
     * 查询缓存数据
     * @param cacheKey 缓存key
     * @return 缓存值
     */
    public String[] getMetaCache(String cacheKey){
        String cacheValue = "-1";
        String cacheFlag = "1";

        DBHelper dbHelper = new DBHelper(context);
        SQLiteDatabase db = dbHelper.open();
        Cursor cursor = db.rawQuery("SELECT cacheValue,updateDate FROM "+REPORT_TABLE_NAME+" WHERE cacheKey = ? and userName = ?", new String[]{cacheKey, userName});
        if(cursor.getCount() > 0 && cursor.moveToFirst()){
            cacheValue = cursor.getString(cursor.getColumnIndex("cacheValue"));
            try {
                Date date = sdf.parse(cursor.getString(cursor.getColumnIndex("updateDate")));
                cacheFlag="1";
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        dbHelper.close(db);
        return new String[]{cacheFlag, cacheValue};
    }
	
	public String checkCacheTime(Date date){
		String cacheFlag = "0";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int hours = date.getHours();
		try {
			Date cacheDate = sdf.parse(sdf.format(date));
			Date today = new Date();
			int todayHours = today.getHours();
			
			today = sdf.parse(sdf.format(today));
			
			int d = (int) ((today.getTime() - cacheDate.getTime()) / (24 * 60 * 60 * 1000));
			if(d > 1){
				cacheFlag = "1";
			} 
//			if(d <= 1 && hours < Constants.UPDATE_HOUR && todayHours >= Constants.UPDATE_HOUR){
//				cacheFlag = "1";
//			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cacheFlag;
	}
	
}
